﻿Imports RunEnv
Imports System.Windows.Forms
Imports System.Data.SqlClient

Public Class frmUsers


    Public Sub ShowOpen()
        Try
            InitUser()
            InitGroup()

            Me.Show()
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub

    Private Sub InitUser()
        Dim sql As String
        Dim dtUser As DataTable
        Dim listViewItem1 As ListViewItem
        sql = "Select UserID,UserName,IsStop" _
            & " From B_User" _
            & " Order By UserID"
        dtUser = CRunEnv.DBConnection.GetDataTable(sql)

        ListView1.Items.Clear()
        ListView1.SmallImageList = ImageList1
        For Each dtRow As DataRow In dtUser.Rows
            listViewItem1 = ListView1.Items.Add(CStr(dtRow.Item("UserID")))
            listViewItem1.SubItems.Add(CStr(dtRow.Item("UserName")))
            If CBool(dtRow.Item("IsStop")) Then
                listViewItem1.ImageIndex = 0
            Else
                listViewItem1.ImageIndex = 1
            End If
        Next

    End Sub

    Private Sub InitGroup()
        Dim sql As String
        Dim dtGroup As DataTable

        sql = "Select UserGroupID " _
            & " From B_UserGroup"
        dtGroup = CRunEnv.DBConnection.GetDataTable(sql)

        ListView2.Items.Clear()
        For Each dtRow As DataRow In dtGroup.Rows
            ListView2.Items.Add(CStr(dtRow.Item("UserGroupID")))
        Next
    End Sub

    Private Sub ToolStrip1_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
        Select Case e.ClickedItem.Text
            Case "新增"
                Select Case Me.TabControl1.SelectedIndex
                    Case 0
                        EditUserAdd()
                    Case 1
                        EditGroupAdd()
                End Select

            Case "编辑"
                Select Case Me.TabControl1.SelectedIndex
                    Case 0
                        EditUserProperty()
                    Case 1
                End Select

            Case "关闭"
                Me.Close()
        End Select
    End Sub

    Private Sub ListView1_DoubleClick(sender As Object, e As System.EventArgs) Handles ListView1.DoubleClick
        EditUserProperty()
    End Sub


    Private Sub EditUserProperty()
        If ListView1.SelectedItems.Count <> 1 Then Exit Sub
        Dim dlg As New dlgUser
        Dim listViewItem1 As ListViewItem
        listViewItem1 = ListView1.SelectedItems(0)
        If dlg.ShowOpen(listViewItem1.Text, listViewItem1.SubItems(1).Text, IIf(listViewItem1.ImageIndex = 0, True, False)) Then
            InitUser()
        End If
    End Sub

    Private Sub EditUserAdd()
        Dim dlg As New dlgUser
        If dlg.ShowOpen() Then
            InitUser()
        End If

    End Sub

    Private Sub EditGroupAdd()
        Dim newGroup As String
        Dim insertCommand As New SqlCommand
        Dim sql As String
        Dim command As SqlCommand
        Dim trans As SqlTransaction

        newGroup = Trim(InputBox("请输入新的组", "新增组"))
        If Len(newGroup) = 0 Then Exit Sub
        trans = CRunEnv.DBConnection.sqlConnection.BeginTransaction
        Try
            sql = "insert into B_UserRight (UserID,RightID,RightValue,IsAllowMana )" _
                    & " select '" & newGroup & "' As UserID,B_URSysFunc.RightID, 0 as RightValue, 0 As IsAllowMana" _
                    & " from B_URSysFunc "
            command = New SqlCommand(sql, CRunEnv.DBConnection.sqlConnection)
            command.Transaction = trans
            command.ExecuteNonQuery()

            insertCommand.Connection = CRunEnv.DBConnection.sqlConnection
            insertCommand.Transaction = trans
            insertCommand.CommandText = "Insert Into B_UserGroup (UserGroupID) " _
                & " Values (@UserGroupID)"
            insertCommand.Parameters.Add("@UserGroupID", SqlDbType.NVarChar)
            insertCommand.Parameters("@UserGroupID").Value = newGroup
            insertCommand.ExecuteNonQuery()

            trans.Commit()

            InitGroup()
        Catch ex As Exception
            trans.Rollback()
            MainException(ex)
        End Try
    End Sub

    Private Sub ContextMenuStripUser_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripUser.ItemClicked
        Select Case e.ClickedItem.Text
            Case "新增"
                EditUserAdd()
            Case "属性"
                EditUserProperty()
            Case "清除口令"
                EditClearPassword()
            Case "隶属关系"
                EditRelation()
        End Select
    End Sub

    Private Sub EditClearPassword()
        Dim userID As String
        Dim sql As String
        Dim command As SqlCommand
        Try
            If ListView1.SelectedItems.Count <> 1 Then Exit Sub
            userID = ListView1.SelectedItems(0).Text
            If MsgBox("是否确定要清除" & userID & "的口令？", _
                      MsgBoxStyle.OkCancel, "系统提示") = MsgBoxResult.Cancel Then Exit Sub
            sql = "Update B_User Set PassWord ='' Where UserID ='" & userID & "'"
            command = New SqlCommand
            command.Connection = CRunEnv.DBConnection.sqlConnection
            command.CommandText = sql
            If command.ExecuteNonQuery > 0 Then
                MsgBox("已清除口令。", MsgBoxStyle.Information, "系统提示")
            End If
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub

    Private Sub ContextMenuStripGroup_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripGroup.ItemClicked
        Select Case e.ClickedItem.Text
            Case "新增"
                EditGroupAdd()
            Case "隶属关系"
                EditRelation()
        End Select
    End Sub

    Private Sub EditRelation()
        Select Case TabControl1.SelectedIndex
            Case 0
                EditRelationUserMap()
            Case 1
                EditRelationUserGroupMap()
        End Select
    End Sub

    Private Sub EditRelationUserMap()
        Dim userID As String
        Dim dlg As dlgUserMap
        If ListView1.SelectedItems.Count <= 0 Then Exit Sub
        userID = ListView1.SelectedItems(0).Text
        dlg = New dlgUserMap
        dlg.ShowOpen(userID)
    End Sub

    Private Sub EditRelationUserGroupMap()
        Dim userGroupID As String
        Dim dlg As dlgUserGroupMap
        If ListView2.SelectedItems.Count <= 0 Then Exit Sub
        userGroupID = ListView2.SelectedItems(0).Text
        dlg = New dlgUserGroupMap
        dlg.ShowOpen(userGroupID)
    End Sub
End Class